<?

include '../includes/specs_functions.php';
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$CICUID = $_GET['CICUID'];

$SESSION = get_session($DB);
if($CICUID == "")
{
	$CICUID = $SESSION->Character;
}
$THEME  = $SESSION->Theme;
$NAME = $SESSION->GetCharacter('name');
$AVATAR = $SESSION->GetCharacter('avatar');
#---------------------------------------------------

Header('content-type: application/x-javascript');


$friends = array();
$users = array();
$users_addIns = array();
/*
$table = "(".TBL_FRIENDS." as fr
			LEFT JOIN
			".TBL_CHARACTERS." as ch
			ON fr.".TBL_FRIENDS_CICUID." = ch.".TBL_CHARACTERS_CICUID.")";

$Where = array("ch.".TBL_CHARACTERS_NAME." <> \"\"");
$col = array(
		"fr.".TBL_FRIENDS_CICUID." as cicuid"
		,"fr.".TBL_FRIENDS_ARRAY." as friends"
		);
		
$orderBy = "fr.".TBL_FRIENDS_CICUID;
$direction = "ASC";

$result = $DB->GetData($table ,$col ,true ,$Where, $orderBy, $direction);

$max = 100;
$i = 0;
foreach($result[3] as $row){

	$users[$row['cicuid']] = $i;
	$friends[$i] = $row['friends'];

$i++;
if($i > $max) 
	break;

}*/


$maxDepth = 0;

$all_friends = getFriends($CICUID ,$DB, 0, $users, $friends, 0);


$users = $all_friends[0];
$friends = $all_friends[1];

/*
print_r($users);
print_r($friends);
*/


$grp = 1;

$thick = 500;

$i = count($users);


foreach ($users as $name => $pos)
{

	//Add user as a node
	if($CICUID == $name)
	{
		$node .= "{nodeName: '".$name."', group:".$pos[0].", fix: '', lol: 0},";
	}else
	{
		$node .= "{nodeName: '".$name."', group:".$pos[0]."},";
	}
	
	
	$grp = 2;
	
	$arr_friends = explode(",",$friends[$pos[0]]);
	foreach ($arr_friends as $friend){
		//Create link to friend
		//lookup friend in user array
		if(array_key_exists($friend, $users)){
			$link .= "{source:".$pos[0].", target:".$users[$friend][0].", value:".$thick."},";
		}else if(array_key_exists($friend, $users_addIns)){
			$link .= "{source:".$pos[0].", target:".$users_addIns[$friend].", value:".$thick."},";
		}else{
			$users_addIns[$friend] = $i;
			$nodeAddIns .= "{nodeName: '".$friend."', group:".$i."},";
			$link .= "{source:".$pos[0].", target:".$i.", value:".$thick."},";
			$i++;
		}
		
	}
	
	if($thick > 1){
		$thick = 2;
	}
	//$thick = floor($thick-2);
}


$node .= $nodeAddIns;

foreach ($users_addIns as $newUser => $pos){
	$users[$newUser] = array($pos, 99);
}
/*


print_r($node);
echo "<br><br><br>";
print_r($link);

*/


print "var miserables = { nodes: [ ".substr($node, 0, -1)." ],  links:[ ".substr($link, 0, -1)." ]	};";


function getFriends($id ,$DB, $depth, $us, $fr, $i){

	$table = "(".TBL_FRIENDS." as fr
				LEFT JOIN
				".TBL_CHARACTERS." as ch
				ON fr.".TBL_FRIENDS_CICUID." = ch.".TBL_CHARACTERS_CICUID.")";

	$Where = array("fr.".TBL_FRIENDS_CICUID." = \"".$id."\"");
	$col = array(
			"fr.".TBL_FRIENDS_CICUID." as cicuid"
			,"fr.".TBL_FRIENDS_ARRAY." as friends"
			);
			
	$orderBy = "fr.".TBL_FRIENDS_CICUID;
	$direction = "ASC";
	
	$result = $DB->GetData($table ,$col ,true ,$Where, $orderBy, $direction);

	foreach($result[3] as $row){
	
		$us[$row['cicuid']] = array($i, $depth);
		$fr[$i] = $row['friends'];
		$i++;
		
		if($depth <= 1){
			$us_fr = explode(",", $row['friends']);

			
			foreach ($us_fr as $friend){
				if(!array_key_exists( $friend, $us))
				{
					$deep_friends = getFriends($friend ,$DB, ($depth+1), $us, $fr, $i);
					$i = $deep_friends[2];
					$us = $deep_friends[0];
					$fr = $deep_friends[1];
				}
			}
		
		}
	}
	return array($us, $fr ,$i);
}




?>